package persistence;

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class FileHandler {
	
//=============================TODO============================================== SERVERFILE HANDLER
	
	public boolean filePresent(String fileName){
		//boolean active=true;
		boolean isPresent = false;
		
		File file = new File(fileName);
		if(file.exists()){
				
				isPresent = true;
			//	wins4.executeAction();
			}
		else {isPresent=false;}

		return isPresent;
	}
	
	public void fileDelete(String fileName){
		File file = new File(fileName);
		file.delete();
	}
	
	
	
//=========================================================================== XML HANDLING
	private String element="";
	/**
	* loads an value from a specified Tag and the xmlDom from the specified file
	*
	* @param fileName The file to load
	* @return element The returned Value of the specified Tag
	* @throws Exception
	*/
	public String readXML(String fileName, String tagName)throws Exception	{
		
		try {
			DocumentBuilderFactory factory  = DocumentBuilderFactory.newInstance();
			DocumentBuilder        builder  = factory.newDocumentBuilder();
			
			Document document = builder.parse(new File(fileName));
			element = document.getElementsByTagName(tagName).item(0).getTextContent();
	                   return element;
	               }
	           catch(Exception e){
	                   throw new Exception("File not loaded",e);
	           }
	               
	       }

//=========================================================================== TXT HANDLING
	public void writeTXT(int WriteThis,char suffix, String filePath)throws Exception{
		FileOutputStream out;
		PrintStream p;
		
		try
        {
                //Neuen OutputStream mit Dateiname + symbol
                out = new FileOutputStream(filePath+"spieler"+suffix+"2server.txt");

                p = new PrintStream( out );

                p.println (WriteThis);

                p.close();
        }
        catch (Exception e)
        {
        	throw new Exception("TextDatei nicht gespeichert",e);
        }
	}
	

}
